﻿
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace XFGameFramework.Localization
{

    public class LocalizationProvider : SettingsProvider
    { 
        private static LocalizationProvider instance;

        public static LocalizationProvider Instance
        {
            get
            {
                if (instance == null)
                    instance = new LocalizationProvider("XFGameFramework/Localization", SettingsScope.Project);

                return instance;
            }
        }

        public LocalizationProvider(string path, SettingsScope scopes, IEnumerable<string> keywords = null) : base(path, scopes, keywords)
        {
        }

        private SerializedObject serializedObject;

        private List<SerializedProperty> serializedProperties;

        public override void OnGUI(string searchContext)
        {  
            if (serializedObject == null)
                serializedObject = new SerializedObject(LocalizationSettings.Instance);

            if (serializedProperties == null)
                serializedProperties = serializedObject.GetAllSerializedProperty();


            serializedObject.Update();

            EditorGUI.BeginChangeCheck();

            foreach (var item in serializedProperties)
            {  
                EditorGUILayout.PropertyField(item);

                if (item.name == "baidu")
                {
                    if (GUILayout.Button("百度翻译开放平台" )) 
                    {
                        Application.OpenURL("https://fanyi-api.baidu.com/");
                    } 
                }
                else if (item.name == "youdao") 
                { 
                    if (GUILayout.Button("有道翻译开放平台" ))
                    {
                        Application.OpenURL("https://ai.youdao.com/");
                    }

                }

            }


            serializedObject.ApplyModifiedProperties();
            if (EditorGUI.EndChangeCheck())
            {
                LocalizationSettings.Save();
            }
        } 
         
    }



    public static class LocalizationProjectSettings
    {

        [SettingsProvider]
        public static SettingsProvider Settings()
        {
            return LocalizationProvider.Instance;
        }

    }
     

}

